<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
    <style>
        table,td{
                border: 1px solid gray;
                border-collapse: collapse;
        }
    </style>
<body>
        <!-- .根据数据创建表格实现隔行变色功能 -->
    <table>

    </table>
    <script>
        let arr = [{ name: 'Jack', age: 18, gender: '男' },
        { name: 'Rose', age: 20, gender: '女' },
        { name: 'Top', age: 22, gender: '男' }, {name: 'mam', age: 23, gender: '女' }]
        str = "";
        arr.forEach(function (item) {
            str+= ` <tr><td>${item.name}</td>
                    <td>${item.age}</td>
                    <td>${item.gender}</td></tr>`
        })
        var table = document.getElementsByTagName("table")[0];
        document.onclick = function(){
            table.innerHTML = str;
            var trs = document.getElementsByTagName("tr");
            var news = [...trs]
            for(var i =0;i<news.length;i++){
                if(i%2==0){
                    news[i].style.backgroundColor = "red";
                }
            }
           
            
        }
    </script>
</body>

</html>